using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;
using System.IO;

public partial class Admin_AddCountry : System.Web.UI.Page
{

    #region Variable
    protected long Id
    {
        get
        {
            object o = ViewState["Id"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["Id"] = value;
        }
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        if (Request.QueryString["id"] != null)
        {
            this.Id = long.Parse(Request.QueryString["id"].ToString());
        }
        LoadData();
        LoadCurent();

    }
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulUtility = (HtmlGenericControl)Master.FindControl("ulUtility");
        ulUtility.Attributes.Add("class", "current");
        HtmlControl divUtility = (HtmlControl)Master.FindControl("divUtility");
        divUtility.Attributes.Add("class", "select_sub show");
        HtmlGenericControl liCountry = (HtmlGenericControl)Master.FindControl("liCountry");
        liCountry.Attributes.Add("class", "sub_show");

    }
    #endregion
    #region LoadData
    private void LoadData()
    {
        ADESSA.Entities.Country _Country = new ADESSA.Entities.Country();
        if (this.Id != long.MinValue && this.Id != 0)
        {
            _Country = CountryService.GetCountry(this.Id);
            if (_Country != null)
            {
                txtName.Value = _Country.Name;
                txtOrder.Value = _Country.Order.ToString();
                if (!string.IsNullOrEmpty(_Country.Img))
                {
                    img.Src = "../photos/" + _Country.Img;
                }
                else { img.Visible = false; }
                
            }
        }
    }
    #endregion
    #region VoidSave and Reset
    protected void btnsave_Click(object sender, EventArgs e)
    {
        ADESSA.Entities.Country _Country = new ADESSA.Entities.Country();
        HtmlImage img = (HtmlImage)Master.FindControl("img");
        if (this.Id != long.MinValue && this.Id != 0)
        {
            _Country = CountryService.GetCountry(this.Id);
        }        
        _Country.Name = txtName.Value.Trim();
        _Country.Order = Utility.ParseToBigint(txtOrder.Value);
        _Country.Enable = true;
        
        if (FUImg.HasFile)
        {
            string path = Server.MapPath(RTISettings.NewsPictureFolder);
            try
            {
                if (_Country.Img != null && File.Exists(Path.Combine(path, _Country.Img)))
                {
                    File.Delete(Path.Combine(path, _Country.Img));
                    img.Src = RTISettings.NewsPictureFolder + "/" + _Country.Img;
                    img.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
            _Country.Img = Utilitys.UploadFileImage(FUImg.PostedFile, path, 100, 100, RTISettings.MaxFileSize, true);
        }
        if (this.Id != long.MinValue && this.Id != 0)
        {

            //UserService.UpdateUser(_User);
            CountryService.UpdateCountry(_Country);
            Response.Redirect("Country.aspx");

        }
        else
        {
           // UserService.CreateUser(_User);
            CountryService.CreateCountry(_Country);
            Response.Redirect("Country.aspx");
        }
    }
    protected void btnexit_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
    #endregion
    
    
}
